package com.company;


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @date : 2022/2/17 10:57
 * 时间方法使用
 */

public class P017_LocalDateTime {

    private static final String DATE_PATTERN = "yyyyMMdd";
    private static final String TIME_PATTERN = "HHmmss";
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {
        // 把LocalDateTime转换成Date
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        System.out.println("instant = " + instant);
        Date date = Date.from(instant);
        System.out.println("date = " + date);

        // 使用LocalDateTime的格式化
        System.out.println("getCurrentDate() = " + getCurrentDate());
        System.out.println("getCurrentTime() = " + getCurrentTime());
        System.out.println("getCurrentDateTime() = " + getCurrentDateTime());
    }

    public static String getCurrentDate() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }

    public static String getCurrentTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(TIME_PATTERN));
    }

    public static String getCurrentDateTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));
    }
}
